//
//  RegisterController.m
//  DaleTou
//
//  Created by wenrong on 2018/5/28.
//  Copyright © 2018年 geren. All rights reserved.
//

#import "RegisterController.h"
#import "NB_View.h"
#import "NSString+GNUtil.h"
#import "AutoTimer.h"
#import "WMOpenProtocolViewController.h"
#import "MBProgressHUD+MJ.h"


@interface RegisterController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNumTxtField;

@property (weak, nonatomic) IBOutlet UITextField *verifiedCodeTxtField;

@property (weak, nonatomic) IBOutlet UITextField *passwordTxtField;

@property (weak, nonatomic) IBOutlet NB_Button *verifiedCodeBtn;

@property (weak, nonatomic) IBOutlet UIButton *agreeBtn;
@property (strong, nonatomic) NSTimer *timer;
@end

@implementation RegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor mainLightGrayColor];
}

- (IBAction)registerBtnClicked:(id)sender {
    if (![NSString isNewPhoneNumber:self.phoneNumTxtField.text]) {
        [MBProgressHUD showError:@"请输入正确手机号"];
        return;
    }
    
    if ([self.verifiedCodeTxtField.text isEqualToString:@""]) {
        [MBProgressHUD showError:@"验证码不能为空"];
        return;
    }
    
    if (self.passwordTxtField.text.length < 6) {
        [MBProgressHUD showError:@"密码不能小于6位"];
        return;
    }
    
    if (!self.agreeBtn.selected) {
        [MBProgressHUD showError:@"请同意用户注册协议"];
    }
    [MBProgressHUD showError:@"验证码错误"];
}

- (IBAction)verifiedCodeBtnClicked:(id)sender {
    if (![NSString isNewPhoneNumber:self.phoneNumTxtField.text]) {
        [MBProgressHUD showError:@"请输入正确手机号"];
        return;
    }
    [self timerWithBtnShow];
    [[AutoTimer sharedInstance] AuotoStart];
}
- (IBAction)agreeBtnEvent:(UIButton *)sender {
    sender.selected = !sender.selected;
}


#pragma mark ----倒计时60秒
- (void)timerWithBtnShow{
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ChangeView) userInfo:nil repeats:YES];
    }
}
- (void)ChangeView{
    
    if ([AutoTimer sharedInstance].isRun) {
        NSString *title = [NSString stringWithFormat:@"%d秒",[AutoTimer sharedInstance].NowTime];
        
        [UIView performWithoutAnimation:^{
            self.verifiedCodeBtn.userInteractionEnabled = NO;
            self.verifiedCodeBtn.backgroundColor = [UIColor grayColor];
            [self.verifiedCodeBtn setTitle:title forState:UIControlStateNormal];
            [self.verifiedCodeBtn layoutIfNeeded];
        }];
    }else{
        [self.timer invalidate];
        self.timer = nil;
        self.verifiedCodeBtn.userInteractionEnabled = YES;
        self.verifiedCodeBtn.backgroundColor = [UIColor colorWithHexString:@"ff8e01"];
        [self.verifiedCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
}

- (IBAction)registerXieyiBtnClicked:(id)sender {
    WMOpenProtocolViewController *web = [WMOpenProtocolViewController openProtocolWithHtml:@"registerProtocol"];
    web.title = @"用户注册协议";
    [self.navigationController pushViewController:web animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
